package com.example.weather_forecast;

import android.util.Log;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
import java.util.List;

public class ParseHandler extends DefaultHandler {

    int citys=0;//标记城市
    Boolean tag;//标记是否解析完一个城市
    private String preTag ="";//标志解析到哪一个节点
    List<WeatherInfo> weatherInfos = new ArrayList<WeatherInfo>();
    WeatherInfo weatherInfo = null;

    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        Log.v("life","开始解析xml文件");
        //初始化
        weatherInfo = new WeatherInfo();
        tag=false;
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
        Log.v("life","结束解析xml文件");
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        if("city".equals(qName)){
            Log.v("life","结束第"+citys+"个城市的解析");
        }
        //完整解析一个城市天气状况
        if(tag) {
            weatherInfos.add(weatherInfo);
            weatherInfo = new WeatherInfo();
            tag=false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        String data = new String(ch, start, length);
        if (preTag != null) {
            if (preTag.equals("temp")) {
                weatherInfo.setTemp(data);
                Log.v("life", "温度为：" + data);
            } else if (preTag.equals("weather")) {
                weatherInfo.setWeather(data);
                Log.v("life", "天气为："+data);
            } else if (preTag.equals("name")) {
                weatherInfo.setName(data);
                Log.v("life","地名为："+ data);
            } else if (preTag.equals("pm")) {
                weatherInfo.setPm(data);
                Log.v("life","pm为："+ data);
            } else if (preTag.equals("wind")) {
                weatherInfo.setWind(data);
                Log.v("life","风级为："+ data);
                tag=true;
            }

            preTag = null;
        }
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        //开始解析book元素的属性
        if(qName!=null) {
            preTag = qName;
            if ("city".equals(qName)) {
                citys++;
                Log.v("life", "开始解析第" + citys + "个城市");
                //不知道属性名，只能遍历所有属性
                int num = attributes.getLength();
                for (int i = 0; i < num; i++) {
                    weatherInfo.setId(attributes.getValue(i));
                    Log.v("life", " 第" + (i + 1) + "个城市的属性值是" + attributes.getValue(i));
                }
            }
        }
    }
    public List<WeatherInfo> getInfosFromXMLbySAX (){
        return weatherInfos;
    }
}
